package com.dianrui.fmi.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.dianrui.fmi.R;
import com.dianrui.fmi.bean.PositionBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class AssetsListAdapter extends BaseAdapter {

    private List<PositionBean> list;
    private Context context;
    private CloseClickListener clickListener ;

    public AssetsListAdapter(Context context, List<PositionBean> list) {
        this.list = list;
        this.context = context;
    }


    public void refresh(List<PositionBean> list) {
        this.list = list;
        this.notifyDataSetChanged();
    }





    @Override
    public int getCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public interface CloseClickListener {
        void onPostionsListCloseClick(String id);
        void onAskPriceClick(String price);
    }

    public void setOnButtonClickListener(CloseClickListener clickListener) {
        this.clickListener = clickListener;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_assets, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        if (null != list && list.size() > 0) {
            final PositionBean positionBean = list.get(position);
            holder.godTxt.setText(positionBean.number);
            holder.godPriceTxt.setText(positionBean.earnings);
            holder.inventoryTxt.setText(positionBean.postion);
            holder.names.setText(positionBean.full_symbol);
            holder.mixPrice.setText(positionBean.mix_price);
            holder.priceTxt.setText(positionBean.price);
            holder.startPrice.setText(positionBean.start_price);
            holder.serviceTxt.setText(positionBean.service_charge);
            holder.createTime.setText(positionBean.createtime);
            holder.advanceTxt.setText(positionBean.total);
            if (positionBean.direction.equals("1")) {
                holder.zfStatusImg.setImageResource(R.mipmap.arrow_up_red);
                holder.godTxt.setTextColor(parent.getContext().getResources().getColor(R.color.red1));
            }else {
                holder.zfStatusImg.setImageResource(R.mipmap.green_arrow_down);
                holder.godTxt.setTextColor(parent.getContext().getResources().getColor(R.color.green_2ab23c));
            }
            if (positionBean.earnings.contains("-")) {
                holder.godPriceTxt.setTextColor(parent.getContext().getResources().getColor(R.color.green_2ab23c));
            }else {
                holder.godPriceTxt.setTextColor(parent.getContext().getResources().getColor(R.color.red1));
            }
            holder.close.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (clickListener != null) {
                        clickListener.onPostionsListCloseClick(positionBean.order_id);
                    }
                }
            });
            holder.askPrice.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (clickListener != null) {
                        clickListener.onAskPriceClick(positionBean.price);
                    }

                }
            });
        }

        return convertView;
    }

    class ViewHolder {
        @BindView(R.id.god_txt)
        TextView godTxt;
        @BindView(R.id.create_time)
        TextView createTime;
        @BindView(R.id.god_price_txt)
        TextView godPriceTxt;
        @BindView(R.id.mix_price)
        TextView mixPrice;
        @BindView(R.id.inventory_txt)
        TextView inventoryTxt;
        @BindView(R.id.service_txt)
        TextView serviceTxt;
        @BindView(R.id.name)
        TextView names;
        @BindView(R.id.start_price)
        TextView startPrice;
        @BindView(R.id.price_txt)
        TextView priceTxt;
        @BindView(R.id.advance_txt)
        TextView advanceTxt;
        @BindView(R.id.zf_status_img)
        ImageView zfStatusImg;
        @BindView(R.id.close)
        Button close;
        @BindView(R.id.ask_price)
        Button askPrice;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
